package cn.com.generaldata.os.service.indicator_evaluation.change_analysis;

import org.apache.commons.lang3.Validate;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 同比增长速度评估指标计算类.
 */
public class SPSCalculateService extends IndicatorEvaluationCalculateService {

	private String GOPIndicatorName = ProvinceIndicatorConstant.GOP_INDICATOR_NAME;
	private String CountryIndicatorName = ProvinceIndicatorConstant.GOP_GROWTH_RATE_INDICATOR_NAME;

	@Override
	protected Double calculate(IndicatorEvaluation indicator,
			IndicatorAccountVO indicatorAccountVO) {
		if (indicator.getSection() == Constants.INDICATOR_LEVEL_NATION) {
			return getNationGOPGrowthRate(indicatorAccountVO,
					indicatorAccountVO.getYear(), CountryIndicatorName);
		} else if (indicator.getSection() == Constants.INDICATOR_LEVEL_PROVINCE) {
			return getResult(indicatorAccountVO);
		} else {
			return getResult(indicatorAccountVO);
		}
	}

	/**
	 * 获取省市级数据.
	 * 
	 * @param indicatorAccountVO
	 * @return
	 */
	@SuppressWarnings("null")
	private Double getResult(IndicatorAccountVO indicatorAccountVO) {
		Double currentGOP = getGOP(indicatorAccountVO, indicatorAccountVO.getYear());
		Validate.isTrue(currentGOP != null);
		Double lastGOP = getGOP(indicatorAccountVO, indicatorAccountVO.getYear() - 1);
		Validate.isTrue(lastGOP != null);

		return (currentGOP / lastGOP - 1) * 100;
	}

	/**
	 * 
	 * @param indicatorAccountVO
	 * @return
	 */
	private Double getGOP(IndicatorAccountVO indicatorAccountVO, int year) {
		return getInputOrAccountingIndicatorValue(indicatorAccountVO, year, GOPIndicatorName);
	}

	private Double getNationGOPGrowthRate(IndicatorAccountVO indicatorAccountVO,
			int year, String name) {

		return getInputOrAccountingIndicatorValue(indicatorAccountVO, year, name);

	}

}
